/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../tools/PacketCreator.h"
#include "PlayerQuests.h"
#include "PlayerNPC.h"
#include "Player.h"
#include "Channel.h"
#include "../net/world/World.h"
#include "../net/world/Guild.h"
#include "../net/world/Guilds.h"

PlayerNPC::PlayerNPC(int npcid, Player* player, int questid) {
	this->questid = questid;
	getnum = 0;
	gettext = "";
	state = 0;
	selected = -1;
	cend = false;
	this->npcid = npcid;
	this->player = player;
	text = "";
	player->setNPC(this);
}

PlayerNPC::~PlayerNPC() {
	player->setNPC(0);
}

void PlayerNPC::sendSimple() {
	player->send(PacketCreator().sendSimple(npcid, text));
	text = "";
}

void PlayerNPC::sendYesNo() {
	player->send(PacketCreator().sendYesNo(npcid, text));
	text = "";
}

void PlayerNPC::sendNext() {
	player->send(PacketCreator().sendBackNext(npcid, text, false, true));
	text = "";
}

void PlayerNPC::sendBackNext() {
	player->send(PacketCreator().sendBackNext(npcid, text, true, true));
	text = "";
}

void PlayerNPC::sendBackOK() {
	player->send(PacketCreator().sendBackNext(npcid, text, true, false));
	text = "";
}

void PlayerNPC::sendOK() {
	player->send(PacketCreator().sendBackNext(npcid, text, false, false));
	text = "";
}

void PlayerNPC::sendAcceptDecline() {
	player->send(PacketCreator().sendAcceptDecline(npcid, text));
	text = "";
}

void PlayerNPC::sendGetText(){
	player->send(PacketCreator().sendGetText(npcid, text));
	text = "";
}

void PlayerNPC::sendGetNumber(int def, int min, int max) {
	player->send(PacketCreator().sendGetNumber(npcid, text, def, min, max));
	text = "";
}

void PlayerNPC::sendStyle(CScriptArray* styles) {
	int size = styles->GetSize();
	int style[100];
	for(int i = 0; i < size; i++ ) {
		int* a = (int*)styles->At(i);
		style[i] = *a;
	}
	player->send(PacketCreator().sendStyle(npcid, text, style, size));
	text = "";
}

void PlayerNPC::addQuest(short questid) {
	player->getQuests()->giveQuest(questid, npcid);
}

void PlayerNPC::endQuest(short questid) {
	player->getQuests()->completeQuest(questid, npcid);
}

void PlayerNPC::setStyle(int id) {
	if(id / 10000 == 0) {
		player->setSkin((char)id);
	}
	else if(id / 10000 == 2) {
		player->setFace(id);
	}
	else if(id / 10000 == 3) {
		player->setHair(id);
	}
	player->send(PacketCreator().updatePlayer(player));
}

void PlayerNPC::createGuild() {
	player->send(PacketCreator().createGuild());
}

void PlayerNPC::disbandGuild() {
	player->getChannel()->getWorld()->getGuilds()->deleteGuild(player->getGuild()->getID());
}

void PlayerNPC::increaseGuildCapacity() {
	player->getChannel()->getWorld()->getGuilds()->increaseCapacity(player);
}

void PlayerNPC::changeGuildEmblem() {
	player->send(PacketCreator().changeGuildEmblem());
}